package redis

import (
	"strconv"
)

const stockPrefix = "goods_stock:goods_id_"

// 同步商品虚拟库存

func SyncGoodsStock(goodsId int, num int) {

	key := stockPrefix + strconv.Itoa(goodsId)
	for i := 0; i < num; i++ {
		Red.RPush(key, goodsId)
	}

}

//es需要就是文本,json格式序列化正好匹配
//序列化方式有哪些:json序列化后就是文本所以用json
// json,protobuf是二进制,xml数据交互,配置文件
//虚拟库存/预扣库存长度

func GetStockLength(goodsId int) int {
	key := stockPrefix + strconv.Itoa(goodsId)
	return int(Red.LLen(key).Val())
}

//出库

func OutStock(goodsId, num int) {
	key := stockPrefix + strconv.Itoa(goodsId)
	for i := 0; i < num; i++ {
		Red.LPop(key)
	}
}
